package jvm.jtacck.db.records;

import java.util.Collections;
import java.util.List;

/**
 * 
 * @author Chris Gamble
 * 
 *         Represents a Courier. A Courier is a specific type of user, who has
 *         additional fields to have. They are associated by a username, they
 *         have a list that contains their route, and they also have the last
 *         known latitude and longitude of the Courier.
 * 
 */
public class Courier extends Record {

    private static final long serialVersionUID = -1417538378931608908L;
    public final User user;
    public final List<ClientSite> route;
    public final double latitude;
    public final double longitude;
    public final ClientSite nextDest;

    /**
     * Constructs a new Courier
     * 
     * @param username
     *            the unique User to this Courier
     *            NON-NULLABLE
     * @param route
     *            A List of ClientSites that this Courier will travel to.
     * @param lat
     *            Double of the latitude global position
     * @param lon
     *            Double of the longitude global position 
     */
    public Courier(User username, List<ClientSite> route, double lat, double lon, ClientSite next) {
        this.user = username;
        this.route = Collections.unmodifiableList(route);
        this.latitude = lat;
        this.longitude = lon;
        this.nextDest = next;
    }
    
    @Override
    public boolean equals(Object o){
        if (o instanceof Courier) {
            Courier other = (Courier) o;
            return
                   checkDoubles(latitude, other.latitude) &&
                   checkDoubles(longitude, other.longitude) &&
                   checkLists(route, other.route) &&
                   user.equals(other.user) &&
                   nextDest.equals(other.nextDest);
        }
        return false;
    }
    
    private strictfp boolean checkDoubles(double l1, double l2) {
            return Math.abs(l1 - l2) < .000000001;
    }
    
    private boolean checkLists(List<ClientSite> s1, List<ClientSite> s2) {
        if (s1 == null) {
            return s2 == null;
        }else {
            return s1.equals(s2);
        }
    }
    
    @Override
    public int hashCode() {
        return user.hashCode() + (new Double(latitude)).hashCode() + (new Double(longitude).hashCode());
    }
    
    public String toString() {
        return user.username + ": " + route + ", " +  nextDest.name + ".";
    }
}